package com.yusheng.tr.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yusheng.constant.CacheConstants;
import com.yusheng.domain.R;
import com.yusheng.service.RedisService;
import com.yusheng.tr.common.core.entity.system.SysLogininfor;
import com.yusheng.tr.common.core.page.PageParam;
import com.yusheng.tr.common.log.annotation.Log;
import com.yusheng.tr.common.log.enums.BusinessType;
import com.yusheng.tr.system.service.ISysLogininforService;
import com.yusheng.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统访问记录
 *
 * @author yusheng
 */
@RestController
@RequestMapping("/logininfor")
//public class SysLogininforController extends BaseController {
public class SysLogininforController{
    @Autowired
    private ISysLogininforService logininforService;

    @Autowired
    private RedisService redisService;

//    @RequiresPermissions("system:logininfor:list")
    @GetMapping("/list")
    public R<PageInfo<SysLogininfor>> list(PageParam<SysLogininfor> logininfor) {
        return R.ok(PageHelper.startPage(
                        logininfor.getPageNum(), logininfor.getPageSize())
                .doSelectPageInfo(() ->  logininforService.selectLogininforList(logininfor.getParam())));
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor) {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

//    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public R remove(@PathVariable Long[] infoIds) {
        return R.ok(logininforService.deleteLogininforByIds(infoIds));
    }

//    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public R clean() {
        logininforService.cleanLogininfor();
        return R.ok();
    }

//    @RequiresPermissions("system:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public R unlock(@PathVariable("userName") String userName) {
        redisService.deleteObject(CacheConstants.PWD_ERR_CNT_KEY + userName);
        return R.ok();
    }

//    @InnerAuth
    @PostMapping
    public R<?> add(@RequestBody SysLogininfor logininfor) {
//        throw new ServiceException("test");
        logininforService.insertLogininfor(logininfor);
        return R.ok();
    }
}
